package DCSource.Size;

import java.util.ArrayList;

import DCSource.Size.Default.Byte.*;
import DCSource.Size.Default.Byte.Byte;
import DCSource.Size.Template.SizeForm;

public class DefaultBytecountConverter {
	private ArrayList<SizeForm> list = new ArrayList<SizeForm>();
	public DefaultBytecountConverter(){
		list.add(new Bit());
		list.add(new Byte());
		list.add(new KiloByte());
		list.add(new MegaByte());
		list.add(new GigaByte());
		list.add(new TeraByte());
		list.add(new PetaByte());
		list.add(new ExaByte());
	}
	public void resetInstance(){
		for (int I=0;I<list.size();I++){
			list.get(I).setValue(0);
			list.get(I).undoPower();
		}
	}
	public SizeForm convertTo(SizeForm from,String id){
		SizeForm found = null;
		for (int I=0;I<list.size();I++){
			if (list.get(I).getName().equalsIgnoreCase(id)){
				found=list.get(I);
				break;
			}
		}
		if (found!=null){
			double Index1 = from.getIndex()*-1;
			double Index2 = found.getIndex();
			double Pow = found.getPower();
			found.setValue(0);
			found.undoPower();
			found.pow(Pow);
			found.setValue((double)from.getValue()/(double)Math.pow(Math.pow(2,Pow),(Index1+Index2)*10));
			return found;
		}
		return from;
	}
	public String getTag(String id){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getName().equalsIgnoreCase(id)){
				return list.get(I).getTag();
			}
		}
		return null;
	}
	public String getName(String tag){
		for (int I=0;I<list.size();I++){
			if (list.get(I).getTag().equals(tag)){
				return list.get(I).getName();
			}
		}
		return null;
	}
}
